package pers.vic.upload.business;

import cn.xuqiudong.common.base.model.BaseResponse;
import oracle.jdbc.proxy.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.vic.upload.business.service.AttachmentService;
import pers.vic.upload.business.service.BaseBusinessService;
import pers.vic.upload.constant.FileType;
import pers.vic.upload.constant.ResultEnum;
import pers.vic.upload.socket.model.RealFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 业务处理的入口
 * @author Vic.xu
 * @date 2022-07-19 10:48
 */
@Component
public class BusinessFacade {

    public static final Logger LOGGER = LoggerFactory.getLogger(BusinessFacade.class);

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private Collection<BaseBusinessService<?>> baseBusinessServices;

    private Map<FileType, BaseBusinessService<?>> businessServiceMap;

    @PostConstruct
    private void post() {
        businessServiceMap = new HashMap<>();
        if (baseBusinessServices == null) {
            LOGGER.warn("没有找到处理对应上传文件的service");
            return;
        }
        for (BaseBusinessService<?> baseBusinessService : baseBusinessServices) {
            businessServiceMap.put(baseBusinessService.type(), baseBusinessService);
        }

    }


    public void handlerBusiness(RealFile realFile) {
        if (realFile == null) {
            return;
        }
        BaseBusinessService<?> baseBusinessService = businessServiceMap.get(realFile.getType());
        if (baseBusinessService == null) {
            LOGGER.warn("没有找到{}类型的文件上传后的处理service", realFile.getType());
            return;
        }
        File file = realFile.file();
        try{
            BaseResponse<?> baseResponse = baseBusinessService.saveAndRelation(file);
            if (!baseResponse.isSuccess()) {
                LOGGER.warn("保存文件失败:" + baseResponse.getMsg() );
            }
        }catch (Exception e) {
            LOGGER.error("保存"+realFile.getType()+"文件后续处理出错", e);
        }
    }

    /**
     * 校验文件合法想
     * @param filename file name
     * @return BaseResponse BaseResponse
     */
    public BaseResponse<?> check(String filename, FileType type, boolean ignoreUnique){
        BaseBusinessService<?> baseBusinessService = businessServiceMap.get(type);
        if (baseBusinessService == null) {
            LOGGER.warn("没有找到{}类型的文件上传后的处理service", type);
            return BaseResponse.error(ResultEnum.INVALID_FILE_TYPE);
        }

        return baseBusinessService.check(filename, ignoreUnique);
    }

    /**
     * test query last 10 record
     * @param type
     * @return
     */
    public BaseResponse<List<Map<String, Object>>> find( FileType type) {
        BaseBusinessService<?> baseBusinessService = businessServiceMap.get(type);
        if (baseBusinessService == null) {
            LOGGER.warn("没有找到{}类型的文件上传后的处理service", type);
            return BaseResponse.error(ResultEnum.INVALID_FILE_TYPE);
        }

        return BaseResponse.success(baseBusinessService.find());
    }
}
